package com.sdp.th.db.th_eq_alarm_log.service.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdp.th.cache.caches.AlarmSetCache;
import com.sdp.th.db.th_alarm_count.service.IThAlarmCountService;
import com.sdp.th.db.th_eq_alarm_log.mapper.ThEqAlarmLogMapper;
import com.sdp.th.db.th_eq_alarm_log.service.IThEqAlarmLogService;
import com.sdp.th.db.th_eq_info.service.IThEqInfoService;
import com.sdp.th.entity.ThEqAlarmLog;
import com.sdp.th.entity.ThEqAlarmSet;
import com.sdp.th.entity.ThEqInfo;
import com.sdp.th.entity.ThEqState;
import com.sdp.th.utils.ThConstants;

/**
 * <p>
 * 设备报警日志信息 服务实现类
 * </p>
 *
 * @author zsp
 * @since 2025-04-13
 */
@Service
public class ThEqAlarmLogServiceImpl extends ServiceImpl<ThEqAlarmLogMapper, ThEqAlarmLog> implements IThEqAlarmLogService {

	
	@Autowired
	private IThEqInfoService iThEqInfoService;

	@Autowired
	private IThAlarmCountService iThAlarmCountService;
	
	@Autowired
	private AlarmSetCache alarmSetCache;
	
	@Override
	@Transactional(rollbackFor=Exception.class)
	public boolean verifyTHAlarm(ThEqState thEqState) {
		
		if(thEqState==null || StringUtils.isEmpty(thEqState.getDeviceNo())) {
			return false;
		}
		
		ThEqAlarmSet alarmSet = alarmSetCache.getAlarmSetCacheByNo(thEqState.getDeviceNo());
	
		if(alarmSet==null) {
			return false;
		}
		
		if(ThConstants.报警策略_不报警.getCode().equals(alarmSet.getTemperatureStrategy()) &&
				ThConstants.报警策略_不报警.getCode().equals(alarmSet.getHumidityStrategy())) {
			return false;
		}
		
		//查询设备基础信息
		ThEqInfo eqInfo = iThEqInfoService.queryEqInfoByNo(thEqState.getDeviceNo());
		List<ThEqAlarmLog> logList = new ArrayList<>();
		
		
		//处理温度
		if(ThConstants.报警策略_超上限.getCode().equals(alarmSet.getTemperatureStrategy())) {
			verifyTemperatureUpper(alarmSet, alarmSet.getTemperatureUpperVal(),thEqState, eqInfo, logList);
		}else if(ThConstants.报警策略_超下限.getCode().equals(alarmSet.getTemperatureStrategy())) {
			verifyTemperatureLower(alarmSet, alarmSet.getTemperatureLowerVal(),thEqState, eqInfo, logList);
		}else if(ThConstants.报警策略_超上下限.getCode().equals(alarmSet.getTemperatureStrategy())) {
			verifyTemperatureUpper(alarmSet, alarmSet.getTemperatureUpperVal(),thEqState, eqInfo, logList);
			verifyTemperatureLower(alarmSet, alarmSet.getTemperatureLowerVal(),thEqState, eqInfo, logList);
		}
		
		//处理湿度
		if(ThConstants.报警策略_超上限.getCode().equals(alarmSet.getHumidityStrategy())) {
			verifyHumidityUpper(alarmSet, alarmSet.getHumidityUpperVal(), thEqState, eqInfo, logList);
		}else if(ThConstants.报警策略_超下限.getCode().equals(alarmSet.getHumidityStrategy())) {
			verifyHumidityLower(alarmSet, alarmSet.getHumidityLowerVal(), thEqState, eqInfo, logList);
		}else if(ThConstants.报警策略_超上下限.getCode().equals(alarmSet.getHumidityStrategy())) {
			verifyHumidityUpper(alarmSet, alarmSet.getHumidityUpperVal(), thEqState, eqInfo, logList);
			verifyHumidityLower(alarmSet, alarmSet.getHumidityLowerVal(), thEqState, eqInfo, logList);
		}
		
		if(logList.size()>0) {
			saveBatch(logList);
			
			//统计报警次数
			iThAlarmCountService.setAlarmThCount(BigDecimal.valueOf(logList.size()));
		}
		
		
		return logList.size()>0;
	}
	
	/**
	 * 温度上限
	 * @param alarmSet
	 * @param thEqState
	 * @param eqInfo
	 * @param logList
	 */
	private void verifyTemperatureUpper(ThEqAlarmSet alarmSet,BigDecimal alarmVal,ThEqState thEqState,ThEqInfo eqInfo,List<ThEqAlarmLog> logList) {
		if(alarmVal==null || thEqState.getDeviceTemperature()==null) {
			return;
		}
		if(thEqState.getDeviceTemperature().compareTo(alarmVal)>0) {
			ThEqAlarmLog alarmLog = new ThEqAlarmLog();
			alarmLog.setDeviceNo(thEqState.getDeviceNo());
			alarmLog.setCreateTime(LocalDateTime.now());
			alarmLog.setAlarmAction(ThConstants.报警处理_未处理.getCode());
			alarmLog.setMonitorVal(thEqState.getDeviceTemperature());
			alarmLog.setAlarmVal(alarmVal);
			alarmLog.setAlarmType(ThConstants.报警类型_温度.getCode());
			alarmLog.setAlarmStrategy(alarmSet.getTemperatureStrategy());
			if(eqInfo!=null) {
				alarmLog.setDeviceRoom(eqInfo.getDeviceRoom());
				alarmLog.setDeviceRegion(eqInfo.getDeviceRegion());
				alarmLog.setDeviceName(eqInfo.getDeviceName());
			}
			alarmLog.setAlarmResultType(ThConstants.报警结果类型_超过上限.getCode());
			logList.add(alarmLog);
		}
		
	}
	
	/**
	 * 温度下限
	 * @param alarmSet
	 * @param alarmVal
	 * @param thEqState
	 * @param eqInfo
	 * @param logList
	 */
	private void verifyTemperatureLower(ThEqAlarmSet alarmSet,BigDecimal alarmVal,ThEqState thEqState,ThEqInfo eqInfo,List<ThEqAlarmLog> logList) {
		if(alarmVal==null || thEqState.getDeviceTemperature()==null) {
			return;
		}
		if(thEqState.getDeviceTemperature().compareTo(alarmVal)<0) {
			ThEqAlarmLog alarmLog = new ThEqAlarmLog();
			alarmLog.setDeviceNo(thEqState.getDeviceNo());
			alarmLog.setCreateTime(LocalDateTime.now());
			alarmLog.setAlarmAction(ThConstants.报警处理_未处理.getCode());
			alarmLog.setMonitorVal(thEqState.getDeviceTemperature());
			alarmLog.setAlarmVal(alarmVal);
			alarmLog.setAlarmType(ThConstants.报警类型_温度.getCode());
			alarmLog.setAlarmStrategy(alarmSet.getTemperatureStrategy());
			if(eqInfo!=null) {
				alarmLog.setDeviceRoom(eqInfo.getDeviceRoom());
				alarmLog.setDeviceRegion(eqInfo.getDeviceRegion());
				alarmLog.setDeviceName(eqInfo.getDeviceName());
			}
			
			alarmLog.setAlarmResultType(ThConstants.报警结果类型_低于下限.getCode());

			logList.add(alarmLog);
		}
		
	}
	
	/**
	 * 湿度上限
	 * @param alarmSet
	 * @param alarmVal
	 * @param thEqState
	 * @param eqInfo
	 * @param logList
	 */
	private void verifyHumidityUpper(ThEqAlarmSet alarmSet,BigDecimal alarmVal,ThEqState thEqState,ThEqInfo eqInfo,List<ThEqAlarmLog> logList) {
		if(alarmVal==null || thEqState.getDeviceHumidity()==null) {
			return;
		}
		
		if(thEqState.getDeviceHumidity().compareTo(alarmVal)>0) {
			ThEqAlarmLog alarmLog = new ThEqAlarmLog();
			alarmLog.setDeviceNo(thEqState.getDeviceNo());
			alarmLog.setCreateTime(LocalDateTime.now());
			alarmLog.setAlarmAction(ThConstants.报警处理_未处理.getCode());
			alarmLog.setMonitorVal(thEqState.getDeviceHumidity());
			alarmLog.setAlarmVal(alarmVal);
			alarmLog.setAlarmType(ThConstants.报警类型_湿度.getCode());
			alarmLog.setAlarmStrategy(alarmSet.getHumidityStrategy());
			if(eqInfo!=null) {
				alarmLog.setDeviceRoom(eqInfo.getDeviceRoom());
				alarmLog.setDeviceRegion(eqInfo.getDeviceRegion());
				alarmLog.setDeviceName(eqInfo.getDeviceName());
			}
			
			alarmLog.setAlarmResultType(ThConstants.报警结果类型_超过上限.getCode());

			logList.add(alarmLog);
		}
		
	}
	
	
	/**
	 * 湿度下限
	 * @param alarmSet
	 * @param alarmVal
	 * @param thEqState
	 * @param eqInfo
	 * @param logList
	 */
	private void verifyHumidityLower(ThEqAlarmSet alarmSet,BigDecimal alarmVal,ThEqState thEqState,ThEqInfo eqInfo,List<ThEqAlarmLog> logList) {
		if(alarmVal==null || thEqState.getDeviceHumidity()==null) {
			return;
		}
		
		if(thEqState.getDeviceHumidity().compareTo(alarmVal)<0) {
			ThEqAlarmLog alarmLog = new ThEqAlarmLog();
			alarmLog.setDeviceNo(thEqState.getDeviceNo());
			alarmLog.setCreateTime(LocalDateTime.now());
			alarmLog.setAlarmAction(ThConstants.报警处理_未处理.getCode());
			alarmLog.setMonitorVal(thEqState.getDeviceHumidity());
			alarmLog.setAlarmVal(alarmVal);
			alarmLog.setAlarmType(ThConstants.报警类型_湿度.getCode());
			alarmLog.setAlarmStrategy(alarmSet.getHumidityStrategy());
			if(eqInfo!=null) {
				alarmLog.setDeviceRoom(eqInfo.getDeviceRoom());
				alarmLog.setDeviceRegion(eqInfo.getDeviceRegion());
				alarmLog.setDeviceName(eqInfo.getDeviceName());
			}
	
			alarmLog.setAlarmResultType(ThConstants.报警结果类型_低于下限.getCode());

			logList.add(alarmLog);
		}
		
	}

	@Override
	public int needHandleAlarmCount() {
		LambdaQueryWrapper<ThEqAlarmLog> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(ThEqAlarmLog::getAlarmAction, ThConstants.报警处理_未处理.getCode());
		return count(queryWrapper);
	}

	@Override
	public List<ThEqAlarmLog> queryTop5AlarmLog() {
		LambdaQueryWrapper<ThEqAlarmLog> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.orderByDesc(ThEqAlarmLog::getCreateTime);
		queryWrapper.last("LIMIT 5");
		return list(queryWrapper);
	}
}
